package com.samp.base;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;

/**
 * idel编译的时候，不会拷贝java源文件下的数据文件，导致编译后的文件读取不到文件。
 */
public class Classpath {
    private static void getResource(){
        URL data01 = Classpath.class.getResource("/data01.txt");
        //class.getResource方法，如果path以/开头，从classpath下查找，同class.getClassLoader()..,
        //如果不是/开关，则是当前java class所有位置开始查找
        printURL(data01);
        URL data02 = Classpath.class.getClassLoader().getResource("com/samp/base/data01.txt");
        printURL(data02);
    }

    private static void printURL(URL url){
        if( url == null ){
            System.out.println("url is null");
            return;
        }
        File file = new File(url.getFile());
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            while ((tempString = reader.readLine()) != null){
                System.out.println(tempString);
            }
            reader.close();
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            if (reader != null) {
                try {
                    reader.close();
                }catch(IOException e1){
                    e1.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
       // getResource();
        //String version = Classpath.class.getPackage().getImplementationVersion();
        //System.out.println(version);
        String string = System.getProperty("java.vm.version");
        System.out.println(string);
        string = "1.8.0_120";
        String revision;
        int i = string.indexOf("-");
        if (i == -1) {
            revision = string;
        } else {
            revision = string.substring(0, i);
        }
        String[] revisionNumbers = revision.split("\\.");
        byte majorRevision = 0;
        byte minorRevision = 0;
        byte maintenanceRevision = 0;
        byte updateRevision = 0;
        String build = null;

        if (revisionNumbers.length > 0) {
            majorRevision = Byte.parseByte(revisionNumbers[0]);

            if (revisionNumbers.length > 1) {
                minorRevision = Byte.parseByte(revisionNumbers[1]);

                if (revisionNumbers.length > 2) {
                    String[] maintenanceRevisionNumbers = revisionNumbers[2].split("_");

                    if (maintenanceRevisionNumbers.length > 0) {
                        maintenanceRevision = Byte.parseByte(maintenanceRevisionNumbers[0]);

                        if (maintenanceRevisionNumbers.length > 1) {
                            updateRevision = Byte.parseByte(maintenanceRevisionNumbers[1]);
                        }
                    }
                }
            }
            System.out.println(majorRevision);
            System.out.println(minorRevision);
            System.out.println(maintenanceRevision);
            System.out.println(updateRevision);
    }
    }
}
